/**
 * @(#)BeanCopier.java 2017年5月22日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service;

import net.sf.cglib.beans.BeanCopier;

/**
 * beancopy
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
public class BaseBeanCopier<F, T> {

	private BeanCopier beanCopier;

	protected BeanCopier getBeanCopier() {
		return beanCopier;
	}

	protected void init() {
		this.beanCopier = BeanCopier.create(sourceClass, targetClass, false);
	}

	private Class<T> targetClass;

	private Class<F> sourceClass;

	protected Class<T> getTargetClass() {
		return targetClass;
	}

	protected Class<F> getSourceClass() {
		return sourceClass;
	}

	public void setTargetClass(Class<T> targetClass) {
		this.targetClass = targetClass;
	}

	public void setSourceClass(Class<F> sourceClass) {
		this.sourceClass = sourceClass;
	}

	public T afterCopy(F source, T target) {
		return target;
	}

	public T copy(F input) {
		try {
			T o = targetClass.newInstance();
			beanCopier.copy(input, o, null);
			return afterCopy(input, o);
		} catch (Exception e) {
			throw new RuntimeException("create object fail, class:" + targetClass.getName() + " ", e);
		}
	}
}
